package com.sixth.service.impl;



import com.sixth.annotation.PageInfoAdvice;
import com.github.pagehelper.util.StringUtil;
import com.sixth.bean.MarketUser;
import com.sixth.bean.MarketUserExample;
import com.sixth.bean.common.BasePageInfo;
import com.sixth.constant.Constant;
import com.sixth.mapper.MarketUserMapper;
import com.sixth.service.AdminUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName AdminUserServiceImpl
 * @Description TODO
 * @Author XiaoWJ
 * @Date 2022/9/6 22:48
 **/
@Service
@Transactional
public class AdminUserServiceImpl implements AdminUserService {

    @Autowired
    MarketUserMapper marketUserMapper;

    @PageInfoAdvice
    @Override
    public Object list(BasePageInfo basePageInfo, String username, String mobile) {
        /*// 开启分页
        Integer limit = adminUserListBo.getLimit();
        Integer page = adminUserListBo.getPage();
        PageHelper.startPage(page, limit);
        */
        MarketUserExample example = new MarketUserExample();
        MarketUserExample.Criteria criteria = example.createCriteria();

        // 如果username不为空，模糊查询username
        if (!StringUtil.isEmpty(username)) {
            criteria.andUsernameLike("%" + username.trim() + "%");
        }

        // 如果mobile不为空，查询匹配
        if (!StringUtil.isEmpty(mobile)) {
            criteria.andMobileEqualTo(mobile);
        }

        example.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());

        List<MarketUser> marketUsers = marketUserMapper.selectByExample(example);

        // PageInfo<MarketUser> pageInfo = new PageInfo<>(marketUsers);
        //
        // //long total = pageInfo.getTotal();
        // //long pages = total / limit + 1;
        // //PageInfo<InnerMarketUserofAdminUserListVo> pageInfo1 = new PageInfo<>();
        //
        // BasePageInfoVo<MarketUser> basePageInfoVo = new BasePageInfoVo<>(pageInfo.getTotal(), pageInfo.getPages(), limit, page, marketUsers);

        return marketUsers;
    }

    @Override
    public MarketUser detail(Integer id) {
        MarketUserExample example = new MarketUserExample();
        MarketUserExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);

        List<MarketUser> marketUsers = marketUserMapper.selectByExample(example);

        if (marketUsers == null || marketUsers.size() == 0) {
            return null;
        }
        return marketUsers.get(0);
    }

    @Override
    public int update(MarketUser marketUser) {
        int affectRows = marketUserMapper.updateByPrimaryKeySelective(marketUser);

        return affectRows > 0 ? Constant.OK : Constant.NON;
    }
}
